from .base import Tool
import os
import subprocess
import platform

def execute(params: dict) -> str:
    """执行open工具"""
    path = params.get("path", "")
    open_type = params.get("type", "file")
    
    if not path:
        return "错误：未提供路径"
    
    try:
        if not os.path.exists(path):
            return f"错误：找不到文件或程序 {path}"
            
        system = platform.system().lower()
        
        if system == "windows":
            if open_type == "file":
                os.startfile(path)
            else:
                subprocess.Popen([path])
        elif system == "darwin":  # macOS
            subprocess.Popen(["open", path])
        else:  # Linux
            subprocess.Popen(["xdg-open", path])
            
        return f"已打开{open_type}: {path}"
        
    except Exception as e:
        return f"打开失败: {str(e)}"

TOOL = Tool(
    name="open",
    description="打开文件或程序",
    parameters={
        "path": {
            "type": "string",
            "description": "要打开的文件或程序路径",
            "required": True
        },
        "type": {
            "type": "string",
            "description": "打开类型(file或program)，默认为file",
            "required": False
        }
    },
    example='''<open>
<path>test.txt</path>
<type>file</type>
</open>''',
    execute_func=execute
) 